package api.homework.two.two;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author lwj
 * @date 2025-03-11
 * @vesion 1.0.0
 */
public class MainTest02 {
    public static void main(String[] args) {
        /**
         * 多个线程共同卖相同的50张票 -- 实现 Callable
         */
        // 创建 Callable 实现类
        SellingTicketsCallable sellingTicketsCallable = new SellingTicketsCallable();

        // 创建 FutureTask 对象
        FutureTask<String> futureTask1 = new FutureTask<>(sellingTicketsCallable);
        FutureTask<String> futureTask2 = new FutureTask<>(sellingTicketsCallable);
        FutureTask<String> futureTask3 = new FutureTask<>(sellingTicketsCallable);

        // 创建线程对象
        Thread threadCallable1 = new Thread(futureTask1);
        threadCallable1.setName("售票员1");
        Thread threadCallable2 = new Thread(futureTask2);
        threadCallable2.setName("售票员2");
        Thread threadCallable3 = new Thread(futureTask3);
        threadCallable3.setName("售票员3");

        // 启动线程
        threadCallable1.start();
        threadCallable2.start();
        threadCallable3.start();

        // 获取结果集
        try {
            System.out.println(futureTask1.get());
            System.out.println(futureTask2.get());
            System.out.println(futureTask3.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}
